有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Tomcat中无cookie的java支持会话#重复

我有一个带有Spring安全框架的Web应用程序,运行在Tomcat8上。现在,我面临一个新的问题,创建一个安卓应用程序,它支持没有cookie的用户会话。我试图找到一些文档like this,并使用HttpUrlConnection进行了尝试(我不知道哪一个更好),这是我的失败代码

public static String jsessionid = SessionIdentifierGenerator.nextSessionId();

public static String  performPostCall(String requestURL,
        HashMap<String, String> postDataParams) {
    Log.d("url = ",requestURL);
    URL url;
    String response = "";
    try {

        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();


        conn.setReadTimeout(45000);
        conn.setConnectTimeout(45000);
        conn.setRequestMethod("POST");
        Log.d("Cookie : ", jsessionid); 
        conn.setRequestProperty("Cookie","JSESSIONID=" + SessionIdentifierGenerator.nextSessionId());
        conn.setDoInput(true);
        conn.setDoOutput(true);
//        conn.connect();


        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));

        writer.flush();

        int responseCode=conn.getResponseCode();
        writer.close();
        os.close();
        System.out.println(".toString() = "+responseCode);
        System.out.println(".HttpsURLConnection.HTTP_OK = "+HttpsURLConnection.HTTP_OK);
        if (responseCode == HttpsURLConnection.HTTP_OK) {
            String line;
            BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line=br.readLine()) != null) {
                response+=line;
            }
        }
        else {
            response="";
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}

private static String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException{
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for(Map.Entry<String, String> entry : params.entrySet()){
        Log.d("entry.getKey() = ",entry.getKey());
        Log.d("entry.getValue() = ",entry.getValue());
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }
    System.out.println("tetstes = "+result.toString());
    return result.toString();
}

此方法用于创建新会话

   public final class SessionIdentifierGenerator {
  private static SecureRandom random = new SecureRandom();

  public static String nextSessionId() {
    return new BigInteger(130, random).toString(32);
  }
}
HashMap<String, String> parameter = new  HashMap<String, String>();
             parameter.put("username", username);
             parameter.put("password", password);

最后一个我称之为performPostCall("http://localhost:8080/login/authenticate?spring-security-redirect=/login/ajaxSuccess", parameter);


共 (0) 个答案